<template>
  <div id="app">

    <div style="padding: 20px">

      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
        <el-form-item label="预约时间" prop="violationDate">
          <el-date-picker clearable
                          v-model="queryParams.startEndTime"
                          type="datetimerange"
                          range-separator="至"
                          value-format="yyyy-MM-dd hh:mm:ss"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实验室名称" prop="courseName">
          <el-input
            v-model="queryParams.labName"
            placeholder="请输入实验室名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="实验室编号" prop="courseName">
          <el-input
            v-model="queryParams.labNo"
            placeholder="请输入实验室编号"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="20">
        <el-col :span="6" v-for="(lab, index) in labs" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>{{ lab.labName }}</span>
              <el-button style="float: right; padding: 3px 0" type="text" @click="handleReserve(lab)">预约</el-button>
            </div>
            <div>
              <p>编号: {{ lab.labNo }}</p>
              <p>类别: {{ lab.labType }}</p>
              <p>位置: {{ lab.location }}</p>
            </div>
          </el-card>
        </el-col>
      </el-row>

    </div>


    <!-- 预约对话框 -->
    <el-dialog title="预约实验室" :visible.sync="dialogVisible" width="40%">
      <el-form :model="selectedLab" ref="selectedLab" size="small" :inline="true" label-width="100px">
        <el-form-item label="实验室编号" prop="labNo">
          <el-input
            v-model="selectedLab.labNo"
            placeholder="请输入实验室编号"
            disabled
          />
        </el-form-item>
        <el-form-item label="实验室名称" prop="labName">
          <el-input
            v-model="selectedLab.labName"
            placeholder="请输入实验室名称"
            clearable
            disabled
          />
        </el-form-item>

        <el-form-item label="预约时间">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间"
            :picker-options="startTimePickerOptions"
            @change="onStartTimeChange">
          </el-date-picker> -
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间"
            :picker-options="endTimePickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmReserve">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {appointmentList} from "@/api/system/lab";
import {addLabBorrow} from "@/api/system/labBorrow";

export default {
  name: 'App',
  data() {
    return {
      labs: [],
      total: 0,
      queryParams:{

      },
      dialogVisible: false,
      selectedLab: {
        startTime: null,
        endTime: null
      },
      form: {},
      startTimePickerOptions: {
        disabledDate: (time) => {
          return time.getTime() < new Date(new Date().setHours(0, 0, 0, 0)); // 只能选择今天及以后的时间
        },
        selectableRange: '07:00:00 - 20:00:00' // 时间范围限制在早上7点到晚上8点
      },
      endTimePickerOptions: {
        disabledDate: (time) => {
          if (this.form.startTime) {
            const startTime = new Date(this.form.startTime);
            const todayStartOfDay = new Date(new Date().setHours(0, 0, 0, 0));

            // 如果是同一天，允许选择当天的时间
            if (time.toDateString() === startTime.toDateString()) {
              return false; // 不禁用同一天的时间
            }

            // 禁用比开始日期早的日期
            return time.getTime() < startTime.getTime();
          }
          return time.getTime() < new Date(new Date().setHours(0, 0, 0, 0)); // 默认只能选择今天及之后的时间
        },
        // 限制时间范围需要大于开始时间最少一个小时，并且小于20点
        selectableRange: '07:00:00 - 20:00:00'
      }
    }
  },
  methods: {
    // 当开始时间发生变化时，校验结束时间
    onStartTimeChange(value) {
      if (value && this.form.endTime) {
        const startTime = new Date(value);
        const endTime = new Date(this.form.endTime);
        if (endTime.getTime() <= startTime.getTime()) {
          this.form.endTime = ''; // 清空结束时间
        }
      }
    },
    handleReserve(lab) {
      this.selectedLab = lab;
      if (this.selectedLab.startTime) {
        this.form.startTime = this.selectedLab.startTime
      }
      if (this.selectedLab.endTime) {
        this.form.endTime = this.selectedLab.endTime
      }
      this.dialogVisible = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // 判断startEndTime是否存在，且数组大小 = 2
      if (this.queryParams.startEndTime && this.queryParams.startEndTime.length === 2) {
        this.queryParams.startTime = this.queryParams.startEndTime[0];
        this.queryParams.endTime = this.queryParams.startEndTime[1];
        this.queryParams.startEndTime[0] = null;
        this.queryParams.startEndTime[1] = null;
      } else {
        this.queryParams.startTime = '';
        this.queryParams.endTime = '';
      }
      this.getLabList(this.queryParams)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.startEndTime = null
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getLabList(param) {
      appointmentList(param).then(response => {
        this.labs = response.rows;
        this.total = response.total;
      });
    },
    confirmReserve() {
      this.form.labId = this.selectedLab.id
      // 二次确认弹框
      this.$confirm('是否确认预约？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addLabBorrow(this.form).then(response => {
          if (response.code === 200) {
            this.$message({
              message: `成功预约${this.selectedLab.labName},请等待管理员审批！`,
              type: 'success'
            });
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            });
          }
          this.dialogVisible = false;
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消预约'
        });
      });
    }
  },
  created() {
    this.getLabList();
  }
}
</script>

<style>
.box-card {
  width: 100%;
  margin-bottom: 20px;
}
</style>
